// Be aware of scanf+cin/printf+cout. Same as other problems.
// A bit dangerous...
#include<bits/stdc++.h>
#define rep(i,a,b) for (int i=(a),i##__END__=(b);i<=i##__END__;++i)
#define drep(i,a,b) for (int i=(a),i##__END__=(b);i>=i##__END__;--i)
using namespace std;
using ll = long long;

void ofile(string s){ // remember!!!
  freopen((s+".in").c_str(),"r",stdin);
  freopen((s+".out").c_str(),"w",stdout);
}

ll dp[200010],c[200010],buc[1000010],res;
int n,a[200010]; // ai <= 1e6, be aware of it

void solve(){
  cin>>n;
  // if (n>10000)return ;
  rep(i,0,n+5)c[i]=dp[i]=a[i]=0;
  buc[0]=-1e18;
  rep(i,1,n)cin>>a[i],buc[a[i]]=-1e18;
  rep(i,2,n)c[i]=c[i-1]+(a[i]==a[i-1])*a[i];
  // rep(i,2,n)cout<<c[i]<<' ';
  // cout<<'\n';

  // rep(i,1,n)drep(j,i-1,0){ // 50pts
  //   dp[i]=max(dp[i],dp[j]+(c[i]-c[j+1])+(a[i+1]==a[j])*a[i+1]);
  //   // if (i==2&&j==1)cerr<<dp[i]<<' '<<c[i]-c[j+1]<<'\n';
  // }
  ll mx=0;
  rep(i,1,n){
    dp[i]=max(mx,buc[a[i+1]]+a[i+1])+c[i];
    buc[a[i]]=max(buc[a[i]],dp[i]-c[i+1]);
    mx=max(mx,dp[i]-c[i+1]);
  }
  // rep(i,1,n)cerr<<dp[i]<<' ';
  // cerr<<'\n';
  res=dp[n];
  rep(i,1,n-1)res=max(res,dp[i]+c[n]-c[i+1]);
  cout<<res<<endl;
}

int main(){
  ofile("color");
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int tt;
  cin>>tt;
  while (tt--)solve();
  cout<<flush;
  return 0;
}